﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    //
    public class QFTAB50Data : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public QFTAB50Data()
            : base()
        {

        }
        public QFTAB50Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public QFTAB50Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }

        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                DataRow rowtemp = null;
                rowtemp = INTEXDB.ABKOPF.NewRow();
                rowtemp["ABKMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABKAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABKAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABKOPF(rowtemp);
                Return.Merge(Result);

                rowtemp = INTEXDB.ABPOS.NewRow();
                rowtemp["ABPMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABPOS(rowtemp, 2);
                Return.Merge(Result);
                string art = "", var = "", color = "";
                string processedyarn = "";
                string processedcolor = "";
                foreach (DataRow row in Return.Tables["ABPOS"].Rows)
                {

                    rowtemp = INTEXDB.ABPR.NewRow();
                    rowtemp["ARMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ARAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ARAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ARAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABPR(rowtemp, 1);
                    Return.Merge(Result);
                    #region processflow=======
                    rowtemp = INTEXDB.ABFOLGE.NewRow();
                    rowtemp["ABFMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ABFAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ABFAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ABFAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABFOLGE(rowtemp, 1);
                    Return.Merge(Result);
                    string processed = "";
                    foreach (DataRow rowf in Result.Tables[0].Rows)
                    {
                        string ABFLIEFNR = rowf["ABFLIEFNR"].ToString().Trim();

                        if (ABFLIEFNR == "") continue;

                        if (processed.Contains(ABFLIEFNR + ";")) continue;

                        processed += ABFLIEFNR + ";";

                        rowtemp = INTEXDB.LIEFDAT.NewRow();
                        rowtemp["LSMANDANT"] = rowf["ABFMANDANT"];
                        rowtemp["LSLIEFNR"] = rowf["ABFLIEFNR"];
                        Result = INTEXDBC.QueryLIEFDAT(rowtemp, 2);
                        Return.Merge(Result); 
                    }
                    #endregion
                    #region====gaauf=====
                    rowtemp = INTEXDB.GAAUF.NewRow();
                    rowtemp["GAMANDANT"] = row["ABPMANDANT"];
                    rowtemp["GAAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["GAAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["GAAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryGAAUF(rowtemp, 2);
                    Return.Merge(Result);
                    #endregion
                    #region======yarn======
                    art = row["ABPART"].ToString();
                    var = row["ABPVAR"].ToString();
                    color = row["ABPFARBE"].ToString();
                    if (!processedyarn.Contains(art + var + ";"))
                    {
                        rowtemp = INTEXDB.GSDAT.NewRow();
                        rowtemp["GSMANDANT"] = row["ABPMANDANT"];
                        rowtemp["GSART"] = row["ABPART"];
                        rowtemp["GSVAR"] = row["ABPVAR"];
                        Result = INTEXDBC.QueryGSDAT(rowtemp);
                        Return.Merge(Result);

                        rowtemp = INTEXDB.GSMATK.NewRow();

                        rowtemp["GSMMANDANT"] = row["ABPMANDANT"];
                        rowtemp["GSMART"] = row["ABPART"];
                        rowtemp["GSMVAR"] = row["ABPVAR"];
                        Result = INTEXDBC.QueryGSMATK(rowtemp, 1);
                        Return.Merge(Result);
                        processedyarn += art + var + ";";
                    }

                    if (!processedcolor.Contains(art + var + color+";"))
                    {
                        rowtemp = INTEXDB.GSCOLOR.NewRow();
                        rowtemp["GSCMANDANT"] = row["ABPMANDANT"];
                        rowtemp["GSCART"] = row["ABPART"];
                        rowtemp["GSCVAR"] = row["ABPVAR"];
                        rowtemp["GSCFARBE"] = row["ABPFARBE"];
                        Result = INTEXDBC.QueryGSCOLOR(rowtemp, 1);
                        Return.Merge(Result);
                        processedcolor += art + var + color+";";
                    }
                    #endregion
                    string order = "";
                    string remark = "";
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + "   ";
                    remark += GetCommentsFromTXTIND("AB3", order).Trim();
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    remark += GetCommentsFromTXTIND("ABP", order).Trim();
                    row["ABPFILLER1"] = remark;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

    }
}